import { requestWithToken } from "@/utils/request";

/**
 * 生成订单
 * @return {Promise}
 */
export function createOrder() {
  return requestWithToken("/member/order/pre", "get");
}

/**
 * 添加收货地址
 * @param address 收货地址信息对象
 * @return {Promise}
 */
export function addAddress(address) {
  return requestWithToken("/member/address", "post", address);
}

/**
 * 获取收货地址列表
 * @return {Promise}
 */
export function getAddressList() {
  return requestWithToken("/member/address", "get");
}

/**
 * 修改收货地址
 * @param id 收货地址id
 * @param address 新的收货地址
 * @return {Promise}
 */
export function updateAddressById(id, address) {
  return requestWithToken(`/member/address/${id}`, "put", address);
}

/**
 * 提交订单
 * @param order 订单对象
 * @return {Promise}
 */
export function submitOrder(order) {
  return requestWithToken("/member/order", "post", order);
}
/**
 * 根据订单id获取订单详情
 * @param id 订单ID
 * @return {Promise}
 */
export function getOrderInfoById(id) {
  return requestWithToken(`/member/order/${id}`, "get");
}

/**
 * 获取订单列表
 * @param page 页码
 * @param pageSize 每页显示多少数据
 * @param orderState 订单状态 0为全部 1为待付款、2为待发货、3为待收货、4为待评价、5为已完成、6为已取
 * @return {AxiosPromise}
 */
export function getOrderList({ page, pageSize, orderState }) {
  return requestWithToken("/member/order", "get", {
    page,
    pageSize,
    orderState,
  });
}
/**
 * 取消订单
 * @param id 订单ID
 * @param cancelReason 取消原因
 * @return {Promise}
 */
export function cancelOrder({ id, cancelReason }) {
  return requestWithToken(`/member/order/${id}/cancel`, "put", {
    cancelReason,
  });
}
/**
 * 删除订单
 * @param ids 订单ID集合
 * @return {AxiosPromise}
 */
export function deleteOrder(ids) {
  return requestWithToken("/member/order", "delete", { ids });
}
/**
 * 确认收货
 * @param id 订单ID
 * @return {AxiosPromise}
 */
export function confirmReceiptGoods(id) {
  return requestWithToken(`/member/order/${id}/receipt`, "PUT");
}
/**
 * 根据订单ID创建新的订单
 * @param id 订单ID
 * @return {AxiosPromise}
 */
export function createOrderById(id) {
  return requestWithToken(`/member/order/repurchase/${id}`, "get");
}
